---
description: Learn how to create a basic NodeJS project with TypeScript.
---

import { Callout } from '@theguild/components'

# Project Setup

In this section, you will learn how to create a basic NodeJS project with TypeScript.

This step will cover the initial Node.js setup required, basic configuration for TypeScript
projects, and how to setup the development scripts.

## Requirements

- Have NodeJS installed on your operation system
  ([instructions](https://nodejs.org/en/download/package-manager)). You can use any recent version
  (18/20/22/24), but, we highly recommend using the stable Node.js 22 LTS release.
- Your favorite terminal configured (you are going to use it a lot!)
- Run `node -v`, `npm -v`, `npx -v` in your terminal and make sure these commands are available for
  use.

## Creating Node.js & TypeScript Project

This tutorial teaches you how to build a NodeJS project from scratch, so the first thing you need to
do is create the directory that'll hold the files for your project:

Open your terminal, navigate to a location of your choice, and run the following commands:

```sh
mkdir hackernews-node-ts
cd hackernews-node-ts
npm init -y
```

This creates a new directory called `hackernews-node-ts` and initializes it with a `package.json`
file.

The `package.json` is the configuration file for the Node.js app you're building. It lists all
dependencies and other configuration options (such as _scripts_) needed for the app.

To add TypeScript supports for your NodeJS project, do the following:

In your project directory, install the packages required to run a TypeScript project:

```sh npm2yarn
npm i -D --save-exact typescript@5.7.2 @types/node@22.10.1 tsx@4.19.2 cross-env@7.0.3
```

This will add the dependencies to your project under the `node_modules` folder, and update the
`package.json`.

The command above installs the following libraries:

- `typescript` is the basic TypeScript language support and compiler.
- `@types/node` is a package that contains the basic TypeScript types for Node.js environment.
- `tsx` is a library that allow you to run `.ts` files directly, without a compilation step to
  JavaScript and automatically re-run your files after you apply file changes.
- `cross-env` allows providing environment variables cross-platform (windows environment variables
  are different 🤷).

Now, initialize a new TypeScript configuration for your project using the following command:

```sh
npx tsc --init
```

This will create a `tsconfig.json` file in your project. The
[`tsconfig.json` is the TypeScript configuration file](https://typescriptlang.org/docs/handbook/tsconfig-json.html),
and it exists per-project. This is where you tell TypeScript compiler which files to compile and how
to compile.

To make it easier to run your project, replace the `"scripts"` section in your `package.json` file
with the following:

```json filename="package.json" {6-7}
{
  "name": "hackernews-node-ts",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "cross-env NODE_ENV=development tsx watch src/main.ts",
    "start": "tsx src/main.ts"
  },
  "devDependencies": {
    "@types/node": "22.10.1",
    "cross-env": "7.0.3",
    "tsx": "4.19.2",
    "typescript": "5.7.2"
  }
}
```

This will allow you to run the following scripts in your project directory:

- `npm run start` - will start the server.
- `npm run dev` - will start the server and restarts it on every change.

Now create the root entry point for your project, by creating a file under `src/main.ts` with the
following:

```ts filename="src/main.ts"
console.log('Hello World!')
```

And to run your server in watch mode, run in your terminal:

```sh npm2yarn
npm run dev
```

<Callout>
  Watch mode tracks your project's files and re-runs your project automatically on a file change.
</Callout>

You should now see some log output regarding your build process, followed by:

```text
Hello World!
```

<Callout>
  You can try to change the script, and notice that the server restarts automatically on every
  change!
</Callout>

The `main.ts` is the project entrypoint - this is where your NodeJS project starts and this runs the
rest of the code.

Congratulations! You now have a ready-to-use project with NodeJS, TypeScript and development scripts
configured.

Let's add Prettier to our project, to make sure our files always look consistent:

```sh npm2yarn
npm i -D --save-exact prettier@3.4.2
```

and run it:

```sh
npx prettier . --write
```

Make sure to run that after each step.

The next step will show you how to setup a basic GraphQL schema and a GraphQL server!
